<template>
    <div class="add">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
            <el-breadcrumb-item>添加商品</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="step">
            <el-steps :active='parseInt(activeName)' finish-status="success">
                <el-step title="基本信息"></el-step>
                <el-step title="商品参数"></el-step>
                <el-step title="商品属性"></el-step>
                <el-step title="商品图片"></el-step>
                <el-step title="商品内容"></el-step>
            </el-steps>
            <div>
                <el-form ref="AddForm" :model="AddForm" label-width="100px" style='height:600px'>
                    <el-tabs :tab-position="tabPosition" v-model="activeName" style="margin-top:20px;">
                        <el-tab-pane label="基本信息" name="0">
                            <el-form-item label="商品名称" prop="goods_name">
                                <el-input v-model="AddForm.goods_name" style="width:50%"></el-input>
                            </el-form-item>
                            <el-form-item label="商品价格" prop="goods_price">
                                <!-- <el-input v-model="AddForm.goods_price" type="number"></el-input> -->
                                <div>
                                    <el-input-number v-model="AddForm.goods_price" :precision="2" :step="0.1" :max="10"></el-input-number>
                                </div>
                            </el-form-item>
                            <el-form-item label="商品重量" prop="goods_weight">
                                <!-- <el-input v-model="AddForm.goods_weight" type="number"></el-input> -->
                                <div>
                                    <el-input-number v-model="AddForm.goods_weight" :precision="2" :step="0.1" :max="10"></el-input-number>
                                </div>
                            </el-form-item>
                            <el-form-item label="商品数量" prop="goods_number">
                                <!-- <el-input v-model="AddForm.goods_number" type="number"></el-input> -->
                                <div>
                                    <el-input-number v-model="AddForm.goods_number" @change="handleChange" :min="1" :max="10" label="描述文字"></el-input-number>
                                </div>
                            </el-form-item>
                            <el-form-item label="商品分类" prop="goods_cat">
                                <el-cascader :options="catelist" :props='cascaderProps' v-model="selectedOptions" @change="handleChange_cat">
                                </el-cascader>
                            </el-form-item>
                        </el-tab-pane>
                        <el-tab-pane label="商品参数" name="1">商品参数</el-tab-pane>
                        <el-tab-pane label="商品属性" name="2">商品属性</el-tab-pane>
                        <el-tab-pane label="商品图片" name="3">

                            <el-upload class="upload-demo" :headers="getToken()" v-model="AddForm.pics" action="http://localhost:8888/api/private/v1/upload" :on-preview="handlePreview" :on-success="uploadDone" :on-remove="handleRemove" :file-list="fileList2" :before-upload='uploadBeforeValid' list-type="picture">
                                <el-button size="small" type="primary">点击上传</el-button>
                                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                            </el-upload>

                        </el-tab-pane>
                        <el-tab-pane label="商品内容" name="4">
                            <!-- <el-input type="textarea" :rows="9" placeholder="请输入商品内容" name="fifth" v-model="textarea"> -->
                            <quill-editor v-model="AddForm.goods_introduce" ref="myQuillEditor" :options="editorOption" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @ready="onEditorReady($event)">
                            </quill-editor>
                            <!-- </el-input> -->
                        </el-tab-pane>
                    </el-tabs>
                </el-form>
                <el-button type="success" style='position:absolute;right:50px;bottom:100px' @click="uploadAll">提交</el-button>
            </div>
            <!-- 大图预览 -->
            <el-dialog title="预览" :visible.sync="previewDialogVisible">
                <img :src="imgSrc" alt="">
            </el-dialog>
        </div>

    </div>
</template>


<script>
import { getCategories,addGoods } from "../../api/index.js";
export default {
  data() {
    return {
      activeName: 0,
      tabPosition: "left",
      AddForm: {
        // 商品名称
        goods_name: "",
        // 选中的商品分类
        goods_cat: 1,
        // 商品价格
        goods_price: "",
        // 商品数量
        goods_number: "",
        // 商品重量
        goods_weight: "",
        // 商品的描述
        goods_introduce: "",
        // 商品的图片
        pics: [],
        // 商品的属性，包含了动态参数 和 静态参数
        attrs: [],
        // 商品描述
        content: null
      },
      editorOption: {},
      //下拉菜单
      selectedOptions: [],
      catelist: [],
      cascaderProps: {
        label: "cat_name",
        children: "children",
        value: "cat_id"
      },
      //图片上传
      fileList2: [],
      uploadImgSrc: "",
      imgSrc: "",
      previewDialogVisible: false
    };
  },
  mounted() {
    getCategories({ type: 3 }).then(res => {
      if (res.meta.status === 200) {
        console.log(res, "catelist");
        this.catelist = res.data;
      }
    });
  },
  methods: {
    handleChange(value) {
      console.log(value);
    //   this.AddForm.goods_cat = value.join(',')
    },
    handleChange_cat(value) {
      console.log(value);
      this.AddForm.goods_cat = value.join(',')
    },
    //图片上传
    //删除图片
    handleRemove(file, fileList) {
      console.log(file, fileList);
      if (!file.response) {
        return;
      }
      var delFile = file.response.data.tmp_path;
      var index = this.AddForm.pics.findIndex(value => {
        return value.pic === delFile;
      });
      this.AddForm.pics.splice(index, 1);
    },
    //图片预览
    handlePreview(file) {
      this.previewDialogVisible = true;
      this.imgSrc = "http://localhost:8888/" + file.response.data.tmp_path;
    },
    uploadDone(response, file, fileList) {
      console.log(response.data.tmp_path);
      this.AddForm.pics.push({pic:response.data.tmp_path});
    },
    uploadAll() {
      console.log(this.AddForm);
      addGoods(this.AddForm).then(res=>{
          console.log(res)
          this.$message({
            type:'success',
            message:res.meta.msg
          })          
        this.$router.push({path:'lists'})
      })
    },
    getToken() {
      var token = localStorage.getItem("mytoken");
      return { Authorization: token };
    },
    //文件上传之前的函数
    uploadBeforeValid(file) {
      //判断文件的大小是否满足要求，如果返回false,则终止当前文件上传操作
      if (file.size > 500 * 1024) {
        this.$message({
          message: "文件大小不能超过500kb",
          type: "warning"
        });
        // 如果返回false.则中止上传操作
        return false;
      } else {
        this.is_show = false;
      }
    },
    //商品内容
    onEditorBlur(quill) {
    //   console.log("editor blur!", quill);
    },
    onEditorFocus(quill) {
    //   console.log("editor focus!", quill);
    },
    onEditorReady(quill) {
    //   console.log("editor ready!", quill);
    },
    
  },
  computed: {
    editor() {
      return this.$refs.myQuillEditor.quill;
    }
  }
};
</script>


<style scoped>
</style>
